package day03;

import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import untils.SensonReading;
import untils.SensonSource;

public class Example3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new SensonSource())
                .keyBy(r -> r.sensonId)
                .process(new KeyedProcessFunction<String, SensonReading, String>() {

                    private  ValueState<Integer> lastTemp;
                    private  ValueState<Long> timerTs;


                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastTemp = getRuntimeContext().getState(
                                new ValueStateDescriptor<Integer>("last" , Types.INT));
                        timerTs = getRuntimeContext().getState(
                                new ValueStateDescriptor<Long>("time",Types.LONG));
                    }

                    @Override
                    public void processElement(SensonReading sensonReading, Context context, Collector<String> collector) throws Exception {
                        Integer senson = null;
                        if(lastTemp.value() != null) { senson = lastTemp.value(); }
                        lastTemp.update(sensonReading.temperature);

                        Long ts = null;
                        if(timerTs != null){
                            ts = timerTs.value();
                        }

                        if(senson != null) {
                            /**
                             * 温度下降且存在定时器
                             */
                            if (sensonReading.temperature < senson && ts != null) {
                                context.timerService().deleteProcessingTimeTimer(ts);
                                timerTs.clear();
                            }

                            /**
                             * 温度上升且不存在定时器
                             */
                            else if (sensonReading.temperature > senson && ts == null) {
                                long oneSecondLater = context.timerService().currentProcessingTime() + 1000L;
                                context.timerService().registerProcessingTimeTimer(oneSecondLater);
                                timerTs.update(oneSecondLater);

                            }
                        }
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                        out.collect("温度传感器 : " + ctx.getCurrentKey() + "连续一秒温度上升!!!");
                        timerTs.clear();
                    }
                })
                .print();

        env.execute();
    }
}
